package first.codeTop.alibaba;

/**
 * @ClassName LC165
 * @description:
 * @author: 荣燊
 * @create: 2021-10-14 16:18
 **/
public class LC165 {
    /**
     * @Author 荣燊
     * @Description
     * @Date 16:19 2021/10/14
     * @Param
     * @return
     **/
    public static int compareVersion(String version1, String version2) {
        String[] ver1 = version1.split("[.]");
        String[] ver2 = version2.split("[.]");
        int[] verInt1 = new int[ver1.length];
        int[] verInt2 = new int[ver2.length];
        for (int i=0; i<ver1.length; i++) {
            verInt1[i] = Integer.parseInt(ver1[i]);
        }
        for (int i=0; i<ver2.length; i++) {
            verInt2[i] = Integer.parseInt(ver2[i]);
        }
        // 开始比较 两版本的数组元素大小
        int j = ver1.length <= ver2.length ? ver1.length : ver2.length;
        for (int i=0; i<j; i++) {
            if (verInt1[i] > verInt2[i]) {
                return 1;
            } else if (verInt1[i] < verInt2[i]){
                return -1;
            }
        }
        if (ver1.length == ver2.length) {
            return 0;
        } else if (ver1.length > ver2.length) {
            for (int i=j; i<ver1.length; i++) {
                if (verInt1[i] > 0) {
                    return 1;
                }
            }
        } else {
            for (int i=j; i<ver2.length; i++) {
                if (verInt2[i] > 0) {
                    return -1;
                }
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        String version1 = "0.1";
        String version2 = "1.1";
//        String[] split = version1.split("[.]");
//        for (String s : split) {
//            System.out.println(s);
//        }
//        System.out.println(split);
        System.out.println(compareVersion(version1, version2));
    }
}
